package com.dearbinge.data.service.impl.collect;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.collect.ShopCollectService;
import com.dearbinge.data.dao.ShopManageMapper;
import com.dearbinge.data.dao.collect.ShopCollectMapper;
import com.dearbinge.data.pojo.collect.ShopCollectDO;
import com.dearbinge.data.pojo.collect.vo.ShopCollectResponseVO;
import com.dearbinge.data.pojo.shopManage.ShopManage;
import com.dearbinge.utils.BeanMapperUtil;
import com.dearbinge.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品收藏
 *
 * @author wes
 * @date 2019/1/10
 */
@Service("shopCollectService")
public class ShopCollectServiceImpl implements ShopCollectService {

    @Autowired
    private ShopCollectMapper shopCollectMapper;
    @Autowired
    private ShopManageMapper shopManageMapper;


    /**
     * 增加收藏
     *
     * @param collect 收藏
     * @return 结果
     */
    @Override
    public Integer insertCollect(ShopCollectDO collect) {
        //判断是否已经收藏过了
        Integer userId = collect.getUserId();
        Integer shopId = collect.getShopId();
        ShopCollectDO result = shopCollectMapper.getByUserIdAndShopId(userId,shopId);
        if (result != null) {
            //收藏过 直接返回1
            return 1;
        }
        return shopCollectMapper.insert(collect);
    }

    /**
     * 修改收藏
     *
     * @param collect 收藏
     * @return 结果
     */
    @Override
    public Integer updateCollect(ShopCollectDO collect) {
        return shopCollectMapper.update(collect);
    }

    /**
     * 删除
     *
     * @param id id
     * @return 结果
     */
    @Override
    public Integer deleteCollect(Integer id) {
        return  shopCollectMapper.deleteByPrimaryKey(id);
    }


    /**
     * 根据id查询收藏
     *
     * @param id id
     * @return 结果
     */
    @Override
    public ShopCollectResponseVO getCollectById(Integer id) {
        ShopCollectDO shopCollectDO = shopCollectMapper.getById(id);
        ShopCollectResponseVO shopCollectResponseVO = new ShopCollectResponseVO();
        if (shopCollectDO != null) {
            BeanUtils.copyProperties(shopCollectDO,shopCollectResponseVO);
            //通过用户拿店铺
            ShopManage shopManage = shopManageMapper.getById(shopCollectDO.getShopId());
            if (shopManage !=null) {
                shopCollectResponseVO.setShopName(shopManage.getShopName());
                shopCollectResponseVO.setShopLOG(shopManage.getShopLOG());
            }
            return shopCollectResponseVO;
        }else {
            return null;
        }
    }


    /**
     * 查询用户的店铺收藏
     *
     * @param userId 用户id
     * @return 收藏集
     */
    @Override
    public Page listCollectByUserId(String userId, Integer current, Integer size) {
        current = PageUtils.getDefaultValues(current, 1);
        size = PageUtils.getDefaultValues(size, 10);
        Page<ShopCollectResponseVO> page = new Page<>(current, size);
        List<ShopCollectDO> list = shopCollectMapper.listCollectByUserId(userId, page);
        List<ShopCollectResponseVO> result = BeanMapperUtil.map(list, ShopCollectResponseVO.class);
        for (ShopCollectResponseVO shopCollectResponseVO : result) {
            ShopManage shopManage = shopManageMapper.getById(shopCollectResponseVO.getShopId());
            if (shopManage !=null) {
                shopCollectResponseVO.setShopLOG(shopManage.getShopLOG());
                shopCollectResponseVO.setShopName(shopManage.getShopName());
            }
        }
        page.setRecords(result);
        Integer total = shopCollectMapper.countCollectByUserId(userId);
        page.setTotal(total);
        return page;
    }

}